package com.shouhuobao.take.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.shouhuobao.station.R;
import com.shouhuobao.station.wxapi.Constants;
import com.shouhuobao.take.model.TakeWhetherChargesBean;

import droid.frame.utils.android.SharedPref;
import droid.frame.view.ViewPagerExt;

/**
 * 代收包裹是否收费
 * Created by lidandan on 2018/2/2.
 */
public class TakeWhetherChargesActivity extends BaseActivity2 implements View.OnClickListener {

    private BaseFragment mFragment1, mFragment2;
    private ViewPagerExt mViewPager;
    private TextView[] mTags = new TextView[2];
    private TakeWhetherChargesBean item = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        item = (TakeWhetherChargesBean) getIntent().getSerializableExtra("item");
        super.onCreate(savedInstanceState);
        if (SharedPref.isFirstOpen(Constants.isShowCostSetTip)) {
            DialogMgr.showFirstSetCostDialog(getContext());
            SharedPref.setFirstOpen(Constants.isShowCostSetTip, false);
        }
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.take_whether_charges_activity);
        super.findViewById();
        getAppTitle().setCommonTitle("代收收费设置");
        mTags[0] = (TextView) findViewById(R.id.free_take_tv);
        mTags[1] = (TextView) findViewById(R.id.paid_take_tv);
        findViewById(R.id.sure_tv).setOnClickListener(this);
        initFragment();
        mViewPager = (ViewPagerExt) findViewById(R.id.view_pager);
        mViewPager.setSupportScroll(false);
        mViewPager.setFragmentItems(getSupportFragmentManager(), mFragment1, mFragment2);
        if (item == null || item.getChargeType() == 1) {
            mViewPager.setCurrentItem(0, false);
            initView(0);
        } else {
            mViewPager.setCurrentItem(1, false);
            initView(1);
        }
        mTags[0].setOnClickListener(this);
        mTags[1].setOnClickListener(this);

    }

    private void initFragment() {
        mFragment1 = new TakeFreeFragment();
        Bundle bundle1 = new Bundle();
        bundle1.putSerializable("item", item);
        mFragment1.setArguments(bundle1);

        mFragment2 = new TakePaidFragment();
        Bundle bundle2 = new Bundle();
        bundle2.putSerializable("item", item);
        mFragment2.setArguments(bundle2);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //无偿代收服务
            case R.id.free_take_tv:
                if (mViewPager.getCurrentItem() == 0) {
                    return;
                }
                mViewPager.setCurrentItem(0);
                initView(0);
                break;
            //有偿代收服务
            case R.id.paid_take_tv:
                if (mViewPager.getCurrentItem() == 1) {
                    return;
                }
                mViewPager.setCurrentItem(1);
                initView(1);
                break;
            //提交选中的选项
            case R.id.sure_tv:
                ((TakePaidFragment) mFragment2).submitInfo(mViewPager.getCurrentItem() + 1);
                break;
        }
    }

    private void initView(int position) {
        if (position == 0) {
            mTags[0].setBackgroundResource(R.drawable.login_btn_selector);
            mTags[0].setTextColor(getResources().getColor(R.color.white));
            mTags[1].setBackgroundResource(R.drawable.takehome_input_bg);
            mTags[1].setTextColor(getResources().getColor(R.color.app_font_blue));
        } else {
            mTags[1].setBackgroundResource(R.drawable.login_btn_selector);
            mTags[1].setTextColor(getResources().getColor(R.color.white));
            mTags[0].setBackgroundResource(R.drawable.takehome_input_bg);
            mTags[0].setTextColor(getResources().getColor(R.color.app_font_blue));
        }
    }

    /**
     * 设置小包或大包自定义的金额
     */
    public void setSmallOrBigPrice(double money, int type) {
        ((TakePaidFragment) mFragment2).setSmallOrBigPrice(money, type);
    }
}
